package cn.yexiaofeng.bookmanager.controller;


import cn.yexiaofeng.bookmanager.beans.BookInfo;
import cn.yexiaofeng.bookmanager.beans.ReturnData;
import cn.yexiaofeng.bookmanager.service.BookInfoService;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 图书控制层
 */
@Controller
public class BookInfoController {

    @Autowired
    BookInfoService service;



    @GetMapping(value = "/getBok")
    public String getBooks(ServletRequest request) {
        List<BookInfo> allBooks = service.getAllBooks();
        System.out.println(allBooks);
        request.setAttribute("books",allBooks);
        return "bookInfo";
    }




    @GetMapping(value = "/querybook")
    public  String queryBook(ServletRequest request){
        Map<String,String> map=new HashMap();
        map.put("bookName",request.getParameter("bookName"));
        map.put("author",request.getParameter("author"));
        BookInfo booksByNameAndAuthor = service.getBooksByNameAndAuthor(map);
        request.setAttribute("getbooks",booksByNameAndAuthor);
        return "openlayer";
    }



    @GetMapping(value = "/getBookByPage")
    public  String getBookByPage(ServletRequest request){
        String curr=request.getParameter("curr");
        String pageSize=request.getParameter("pageSize");
        if (curr==null||curr==""){
            curr="1";
        }
        if (pageSize==null||pageSize==""){
            pageSize="3";
        }
        List<BookInfo> booksByPage = service.getBooksByPage(curr, pageSize);
        PageInfo pageInfo=new PageInfo(booksByPage,5);
        request.setAttribute("bookpages",pageInfo);
        return "bookInfo";
    }

    @RequestMapping("/addInfo")
    @ResponseBody
    public  Object addInfo(ServletRequest request){
        String book_name = request.getParameter("book_name");
        String author2 = request.getParameter("author2");
        String book_num=request.getParameter("book_num");
        String number2 = request.getParameter("number2");
        String yuding = request.getParameter("yuding");

        BookInfo bookInfo=new BookInfo();
        bookInfo.setBook_name(book_name);
        bookInfo.setAuthor(author2);
        bookInfo.setBook_num(book_num);
        bookInfo.setNumber(Integer.parseInt(number2));
        bookInfo.setYuding(yuding);
        Integer out = service.addInfo(bookInfo);
        ReturnData returnData=new ReturnData();
        if (out!=null){
            returnData.setCode(1);
        }else {
            returnData.setCode(0);
        }
        return JSON.toJSONString(returnData);
    }


    @PostMapping("/deleInfo")
    @ResponseBody
    public  Object deleInfo(ServletRequest request){
        String id = request.getParameter("id");
        ReturnData returnData=new ReturnData();
        if (service.deleBook(Integer.parseInt(id))!=null) {
            returnData.setCode(1);
        }

        return  JSON.toJSONString(returnData);

    }



}